package cn.zcscloud.mall.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * ClassName: R
 * Description:
 * date: 2022/2/2 20:59
 *
 * @author zjf95
 * @since JDK 1.8
 */

@Data
@ApiModel("返回结果")
public class R<T> implements Serializable {

    @ApiModelProperty("编码")
    private Integer code; //编码：1成功，0和其它数字为失败

    @ApiModelProperty("错误信息")
    private String message; //错误信息

    @ApiModelProperty("数据")
    private T data; //数据

    //成功则传入数据
    public static <T> R<T> success(String message,T object) {
        R<T> r = new R<T>();
        if (message == null){
            r.message = "操作成功";
        }
        r.message = message;
        r.data = object;
        r.code = 200;
        return r;
    }

    //失败则传入错误信息
    public static <T> R<T> error(String msg) {
        R r = new R();
        r.message = msg;
        r.code = 0;
        return r;
    }

}
